package com.ccx.coachsite.util;

import android.content.Context;
import android.content.SharedPreferences;

public class SharedPreferencesUtil {

	private static final String PREFERENT_NAME_DEFAULT = "preferent0x";

	public static String getValue(Context context, String key) {
		return getValue(context, key, PREFERENT_NAME_DEFAULT);
	}

	public static String getValue(Context context, String key,
			String preferentName) {
		if (StringUtil.isEmpty(preferentName)) {
			Log.w("preferentName is null...");
			preferentName = PREFERENT_NAME_DEFAULT;
		}
		SharedPreferences settings = context.getSharedPreferences(
				preferentName, Context.MODE_PRIVATE);
		return settings.getString(key, "");
	}

	public static void setValue(Context context, String key, String value) {
		setValue(context, key, value, PREFERENT_NAME_DEFAULT);
	}

	public static void setValue(Context context, String key, String value,
			String preferentName) {

		if (StringUtil.isEmpty(preferentName)) {
			Log.w("preferentName is null...");
			preferentName = PREFERENT_NAME_DEFAULT;
		}

		SharedPreferences settings = context.getSharedPreferences(
				preferentName, Context.MODE_PRIVATE);
		SharedPreferences.Editor editor = settings.edit();
		editor.putString(key, value);
		editor.commit();
	}

	public static boolean getBoolean(Context context, String key,
			String preferentName) {
		if (StringUtil.isEmpty(preferentName)) {
			Log.w("preferentName is null...");
			preferentName = PREFERENT_NAME_DEFAULT;
		}
		SharedPreferences settings = context.getSharedPreferences(
				preferentName, Context.MODE_PRIVATE);
		return settings.getBoolean(key, Boolean.FALSE);
	}

	public static boolean getBoolean(Context context, String key) {
		return getBoolean(context, key, PREFERENT_NAME_DEFAULT);
	}

	public static void setBoolean(Context context, String key, Boolean value) {
		setBoolean(context, key, value, PREFERENT_NAME_DEFAULT);
	}

	public static void setBoolean(Context context, String key, Boolean value,
			String preferentName) {
		if (StringUtil.isEmpty(preferentName)) {
			Log.w("preferentName is null...");
			preferentName = PREFERENT_NAME_DEFAULT;
		}
		SharedPreferences settings = context.getSharedPreferences(
				preferentName, Context.MODE_PRIVATE);
		SharedPreferences.Editor editor = settings.edit();
		editor.putBoolean(key, value.booleanValue());
		editor.commit();
	}

	public static int getInt(Context context, String key, int defValue) {
		SharedPreferences settings = context.getSharedPreferences(
				PREFERENT_NAME_DEFAULT, Context.MODE_PRIVATE);
		return settings.getInt(key, defValue);
	}

	public static void setInt(Context context, String key, int value) {
		SharedPreferences settings = context.getSharedPreferences(
				PREFERENT_NAME_DEFAULT, Context.MODE_PRIVATE);
		SharedPreferences.Editor editor = settings.edit();
		editor.putInt(key, value);
		editor.commit();
	}
}